package com.class05.controller;

import com.class05.pojo.Order;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2023/12/15 8:35
 */
@RestController //==@Controller+@ResponseBody
@RequestMapping("order")
public class OrderController {
    @GetMapping("data")
//    @RequestMapping(value = "data",method = RequestMethod.GET)
    public String data() {
        System.out.println("OrderController.data");
        return "ok";
    }

    @RequestMapping(value = "data2", method = RequestMethod.GET)
    public String data2() {
        System.out.println("OrderController.data2");
        return "ok";
    }
    @PostMapping("getOrder")
    public Object getOrder(@Validated @RequestBody Order order,
                          BindingResult result){
        if (result.hasErrors()) {
//            绑定错误，就不直接返回了，由我们自己决定
            Map data = new HashMap();
            data.put("code", 400);
            data.put("msg", "参数效验异常了！");
            return data;

        }
        System.out.println(order);
        return order;
    }
}
